

package codename.carrot.bomberman.views.widgets;

import sheep.graphics.Image;
import sheep.gui.WidgetAction;
import sheep.math.BoundingBox;
import sheep.math.Vector2;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.MotionEvent;
import codename.carrot.bomberman.AppStorage;
import codename.carrot.bomberman.BitmapFactory;


/**
 * Class handling interactions with the GUI's bomb button.
 * @author Group A4
 */
public class BombButton extends BombermanWidget {    

    /**
     * Image used to show the button in the GUI
     */
	private Image image = BitmapFactory.buildBombButtonUp();
	/**
	 * Image used to display when button is pressed
	 */
	private Image imageButtonDown = BitmapFactory.buildBombButtonDown();
	/**
	 * Image used to display when button is released
	 */
	private Image imageButtonUp = BitmapFactory.buildBombButtonUp();
	/**
	 * The button's position on screen
	 */
    private Vector2 position = new Vector2((float)(AppStorage.SCREEN_WIDTH_GAME-image.getWidth()*1.5), (float)(AppStorage.SCREEN_HEIGHT_GAME-image.getHeight()*1.5));
    /**
     * The button's bounding box that limits the surface where the user can interact with it. 
     */
    private BoundingBox bounds;
  
	/**
	 * Class constructor. Sets the bounding box. 
	 */
    public BombButton() {
        this.bounds = setBoundingBox();
    }
    
    /**
     * Used by the constructor to set the button's bounding box. 
     * @return BoundingBox(bounds) - the object's new bounding box. 
     */
    private BoundingBox setBoundingBox() {
        Rect bounds = new Rect();
        
        bounds.left = (int) position.getX();
        bounds.right= (int)(position.getX()+image.getWidth());
        bounds.top = (int) position.getY();
        bounds.bottom = (int)(position.getY()+image.getHeight());
              
        return new BoundingBox(bounds);
    }
    
    @Override
    public void draw(Canvas canvas) {
    	if(canvas == null) return;
    	image.draw(canvas, position.getX(), position.getY());
    }

    @Override
    public boolean onTouchDown(MotionEvent event) { 
    	super.onTouchDown(event);
        if(bounds.contains(event.getX(), event.getY())) {
        	image = imageButtonDown;
            super.alertListeners((new WidgetAction(this)));
            return true;
        }        
        return false;
    }
    
    @Override
    public boolean onTouchUp(MotionEvent event){
    	if(image != imageButtonUp)
    		image = imageButtonUp;
    	return false;
    }
    
}